# !/usr/bin/python
# -*- coding: utf-8 -*-
# @Time    : 2022/1/17
# @Author  : szw
# @Email   : 1259577135@qq.com
# @File    : util
# @Software: 食物主义
# @desc : 工具类
import  re

import pymongo
from bson import ObjectId
from random import choice
from concurrent.futures import ThreadPoolExecutor
import json
from urllib.parse import parse_qs, urlparse
from urllib.parse import urlencode
import  random
from tqdm import  tqdm
import  time
import configparser

def get_config(config_path="config.ini",group="config"):
    """
    读取配置文件里面的配置 转成字典返回
    :param config_path:
    :return:
    """
    cf = configparser.ConfigParser()
    cf.read(config_path,encoding="utf-8")

    config_dict={}
    for k,v in cf.items(group):
        config_dict[k]=v

    return config_dict


class  tqdm_util():

    def __init__(self,total:int,desc=None):
        if total is None:
            total = 100
        self.tq=tqdm(total=total)
        if desc :
            self.desc=desc
        else :
            self.desc = "当前更新进度"
        self.tq.set_description_str(desc)

    def update(self,_update:int):
        self.tq.set_description_str(f"当前时间:{now()} -------- {self.desc} ")
        self.tq.update(_update)

    def set_desc(self,desc:str):
        self.tq.set_description_str(desc)

    def set_total(self,total:int):
        self.tq.total=total

def thread_util(func,seedList,threadNum=6):
    executor = ThreadPoolExecutor(max_workers=threadNum)
    result = [data for data in executor.map(func, seedList)]
    executor.shutdown()

def camel_to_snake(string):
    string = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', string)
    string = re.sub('(.)([0-9]+)', r'\1_\2', string)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', string).lower()

#驼峰转蟒蛇命名
def hump_to_snake(dict):
    result={}
    for k,v in dict.items():
        result[camel_to_snake(k)] = v

    return result


def get_kuai_proxy():
    # 隧道域名:端口号
    tunnel = "t563.kdltps.com:15818"
    ## 用户名密码方式
    username = "t17055604523596"
    password = "845dth2u"

    proxies = {
        "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
        "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}
    }
    return proxies


def  get_proxy() :
    # 代理ip
    import random
    port = random.randint(24000, 24400)
    proxies = {'http': f'http://zheng123:zheng123@wz1.iinti.cn:{port}',
               'https': f'http://zheng123:zheng123@wz1.iinti.cn:{port}'}
    return proxies


def getLocalProxy():
    return {'http': 'http://localhost:8889', 'https': 'http://localhost:8889'}

import  requests
def download_file(url,path):

    s=requests.session()
    down_res = s.get(url,stream=True,proxies=get_proxy())
    with open(path, "wb") as code:
        code.write(down_res.content)



def subStr(s:str,startStr:str,endStr:str)->str :
    resultStr=s[s.index(startStr)+1:-1]
    resultStr=resultStr[0:resultStr.rindex(endStr)]
    return  resultStr

def url2param(url:str):
    """
    传入url   打印出 参数    因为我们经常会遇到 分析页面的时候   url里一大堆参数的情况 这个方法会辅助分析
    :param url:
    :return:
    """
    # 提取url参数
    query = urlparse(url).query  # wd=python&ie=utf-8
    # 将字符串转换为字典
    params = parse_qs(query)  # {'wd': ['python'], 'ie': ['utf-8']}
    """所得的字典的value都是以列表的形式存在，若列表中都只有一个值"""
    result = {key: params[key][0] for key in params}  # {'wd': 'python', 'ie': 'utf-8'}
    # 顺便打印出来
    return result



def now():
    now_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    return now_date

def today():
    today_date = time.strftime("%Y-%m-%d", time.localtime())
    return today_date

class mongo_manager():

    def __init__(self, collect_name, client="mongodb://172.23.130.66:27017/", db="zhihu"):
        self.client=pymongo.MongoClient(client)
        self.db=self.client[db]
        self.collect=self.db[collect_name]

    def getCollect(self,collect_name:str)->pymongo.collection.Collection :
        """
        创建集合
        :return:
        """
        if bool(collect_name):
            return self.db[collect_name]
        return  self.collect


    def findById(self,id:str):
        myquery = {"_id": ObjectId(id)}
        return self.collect.find_one(myquery)


    def findAll(self,data=None):
        if bool(data):
            return self.collect.find(data)
        return  self.collect.find()

    def findOne(self,data=None):
        if bool(data):
            return self.collect.find_one(data)
        return  self.collect.find_one()


    def find_one_and_update(self,filter,update_data):
        # pymongo.ReturnDocument.AFTER 表示 返回修改后的文档
        result =self.collect.find_one_and_update(filter=filter,update= {'$set': update_data},return_document=pymongo.ReturnDocument.AFTER)
        return result


    def insertOne(self,data:dict):
        return self.collect.insert_one(data)

    def insertMany(self,datas:list):
        return  self.collect.insert_many(datas)

    def updateOne(self,query,new_value):
        newvalues = {"$set": new_value}
        return self.collect.update_one(query, newvalues)

    def updateMany(self, query, new_value):
        newvalues = {"$set": new_value}
        return self.collect.update_many(query, newvalues)

    # def deleteById(self,id):
    #     return self.collect.remove({"_id", ObjectId(id)})

    def  deleteOne(self,myquery):
        return  self.collect.delete_one(myquery)

    def deleteMany(self,myquery):
        return  self.collect.delete_many(myquery)

    def close(self):
        self.client.close()

    def not_exist(self, query):
        if bool(query):
            data = self.collect.find_one(query)
            if  data:
                return False
            return True


import  time
import datetime
def cal_time(stamp1,stamp2):
    t1=time.localtime(stamp1)
    t2 = time.localtime(stamp2)
    t1=time.strftime("%Y-%m-%d %H:%M:%S",t1)
    t2 = time.strftime("%Y-%m-%d %H:%M:%S", t2)
    time1=datetime.datetime.strptime(t1,"%Y-%m-%d %H:%M:%S")
    time2 = datetime.datetime.strptime(t2, "%Y-%m-%d %H:%M:%S")
    return (time2-time1).seconds


def format_timestamp(time_stamp):
    t1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_stamp))
    return t1

topic_mongo = mongo_manager(collect_name="topic")
if __name__ == '__main__':

    f = open("zh_topic_1", encoding="utf-8")

    for line in f.readlines():
        topic = {"topic_id": int(line.split("\t")[0]), "status": "待抓取"}
        if topic_mongo.not_exist({"topic_id": int(line.split("\t")[0])}):
            print(topic)
            topic_mongo.insertOne(topic)
    f.close()